package com.herenit.beijing.dh2011.business.empi;

import javax.ejb.Stateless;

import com.herenit.beijing.dh2011.business.base.BaseManager;
import com.herenit.beijing.dh2011.dto.PageObject;
import com.herenit.beijing.dh2011.entity.PatientIndex;
import java.util.List;

@Stateless
public class PatientIndexManager extends BaseManager<PatientIndex> implements
        IPatientIndexManager {

    @Override
    public void newIndex(PatientIndex patientIndex) {
        em.persist(patientIndex);
    }

    @Override
    public PageObject findPageObjectByPage(PageObject po) {
        po.setTotalRecord(findCountByPage(po));
        po.setTotalPage(po.getTotalPage());
        System.out.println(po);
        return po;
    }

    @Override
    public List<PatientIndex> findByPage(PageObject po) {
        return em.createQuery("select p from PatientIndex p").setHint("org.hibernate.cachable", true).setFirstResult(po.getBeginIndex()).setMaxResults(po.getPageSize()).getResultList();
    }

    @Override
    public long findCountByPage(PageObject po) {
        return (Long) em.createQuery("select count(p) from PatientIndex p").getSingleResult();
    }
}
